/*
 *	Build scene with canvas
 */
define(function() {
	var Scene = function(SceneConf) {
		// Initialize scene according to configuration
		for(property in SceneConf) {
			this[property] = SceneConf[property];
		}
		
		// Create Canvas
		this.canvas = document.createElement('canvas');
		// Configure it
		this.canvas.id = SceneConf.id;

		this.coef = SceneConf.coef;

		// Set scene size
		this.setSize();
		this.setOrigin();

		// Add it to page
		var body = document.getElementsByTagName("body")[0];
		body.appendChild(this.canvas);

		if(SceneConf.isBuffer === true) {
			// Create Pre-Render Canvas
			this.setBuffer(body);		

			window.ctx = this.ctx = this.buffer.getContext('2d');
			this.realctx = this.canvas.getContext('2d');
		} else {
			window.ctx = this.ctx = this.canvas.getContext('2d');
		}

		// Factor for zooming the scene
		this.zoom = isMobileDevice() === true ? 0.5 : 1;
		this.orientation = (window.orientation==90 || window.orientation==-90) ? "landscape" : "portrait";
	}

	Scene.prototype = {
		scale : function(transform) {
			return {
				x : transform.scale.x * this.unite,
				y : transform.scale.y * this.unite
			}
		},
		setSize : function() {
			// Get current size
			this.height	= getWindowHeight()/this.coef;
			this.width 	= getWindowWidth()/this.coef;

			// Set canvas size
			this.canvas.width = this.width*this.coef;
			this.canvas.height = this.height*this.coef;
		},
		setBuffer : function(body) {
			this.buffer = document.createElement('canvas');
			this.buffer.id = this.canvas.id + "-buffer";
			this.buffer.style.visibility = "hidden";

			this.buffer.width = this.width;
			this.buffer.height = this.height;

			body.appendChild(this.buffer);
		},
		setOrigin : function() {
			this.origin = {
				x :  this.width/2/this.zoom,
				y :  this.height/2/this.zoom,
			}
		},
		debugOrigin : function() {
			drawLine(this.ctx,"rgba(255,0,0,.8)",this.width/2,0,this.width/2,this.height);
			drawLine(this.ctx,"rgba(255,0,0,.8)",0,this.height/2,this.width,this.height/2);
		},
		debugPoint : function(x,y) {
			drawArc(
				this.ctx,
				"rgba(255,0,0,.5)",
				x,y,
				5,
				0, Math.PI*2, 2
			);
		},
		capture : function() {
			var quality = isMobileDevice() === true ? 0.5 : 0.3;
			return this.canvas.toDataURL("image/jpeg", quality);
		}
	}

	return Scene;
});